<?php

/**
 * Created by IntelliJ IDEA.
 * User: jirapatjantsakool
 * Date: 3/7/14
 * Time: 12:24 AM
 */
require('../model/user.php');

require('../db/database.php');
require('../service/common_service.php');

class user_service extends common_service
{

    public function findAll()
    {
        global $db;
        $sql = "select * FROM cf_user";
        $query = $db->query($sql);

        $user_list = array();

        while ($result = $db->fetch_array($query)) {

            $user = new user();

            $user->setId($result['id']);
            $user->setUsername($result['username']);
            $user->setPassword($result['password']);
            $user->setDisplayname($result['displayname']);
            $user->setEmail($result['email']);
            $user->setFirstname($result['firstname']);
            $user->setLastname($result['lastname']);
            $user->setAge($result['age']);
            $user->setBirthday(apputil:: getDateStringFormat($result['birthday'] , 'd/m/Y' ));
            $user->setUsertype($result['usertype']);
            $user->setStatus($result['status']);

            $user_list[] = $user;
        }
        return $user_list;
    }

    public function findUserById($id)
    {
        global $db;
        $user = new user();

        $sql = "SELECT "
            . " username, password, displayname, email, "
            . " firstname, lastname, age, birthday, usertype, status "
            . " FROM cf_user "
            . " WHERE id = ? ";

        if ($stmt = $db->prepare($sql)) {

            //binding param
            mysqli_stmt_bind_param($stmt, 'i', $id);

            $db->execute_statement($stmt);

            $rowMaps = array();
            mysqli_stmt_bind_result($stmt, $rowMaps['username'],
                                           $rowMaps['password'],
                                           $rowMaps['displayname'],
                                           $rowMaps['email'],
                                           $rowMaps['firstname'],
                                           $rowMaps['lastname'],
                                           $rowMaps['age'],
                                           $rowMaps['birthday'],
                                           $rowMaps['usertype'],
                                           $rowMaps['status']);

            //TODO check number record
            if ($db->fetch_statement($stmt)) {

                $user->setId($id);
                $user->setUsername($rowMaps['username']);
                $user->setPassword($rowMaps['password']);
                $user->setDisplayname($rowMaps['displayname']);
                $user->setEmail($rowMaps['email']);
                $user->setFirstname($rowMaps['firstname']);
                $user->setLastname($rowMaps['lastname']);
                $user->setAge($rowMaps['age']);
                $user->setBirthday(apputil:: getDateStringFormat($rowMaps['birthday'] ,'d/m/Y' ));
                $user->setUsertype($rowMaps['usertype']);
                $user->setStatus($rowMaps['status']);

            }
            $db->close_connection();
        }
        return $user;
    }

    public function delete($user) {
        global $db;
        static $flag = false;

        $sql = "DELETE FROM cf_user "
            . " WHERE id = ? ";

        if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 'i', $user->getId());

            $flag = $db->execute_statement($stmt);
        }
        $db->close_connection();
        return $flag;
    }

    public function update($user) {
        global $db;
        static $flag = FALSE;

        if ($user->getPassword() != "" || $user->getPassword() != null) {

            $sql = "UPDATE cf_user SET "
                . " username = ?, password = ?, displayname = ?, email = ?, "
                . " firstname = ?, lastname = ?, age = ?, birthday = STR_TO_DATE(?, '%d/%m/%Y/'), usertype = ?, status = ? "
                . " WHERE id = ? ";

            if ($stmt = $db->prepare($sql)) {
                //binding param
                mysqli_stmt_bind_param($stmt, 'ssssssisiii', $user->getUsername(),
                                                             crypt($user->getPassword()),
                                                             $user->getDisplayname(),
                                                             $user->getEmail(),
                                                             $user->getFirstname(),
                                                             $user->getLastname(),
                                                             $user->getAge(),
                                                             $user->getBirthday(),
                                                             $user->getUsertype(),
                                                             $user->getStatus(),
                                                             $user->getId());
                $flag = $db->execute_statement($stmt);
            }
            $db->close_connection();

        } else {

            $sql = "UPDATE cf_user SET "
                . " username = ?, displayname = ?, email = ?, "
                . " firstname = ?, lastname = ?, age = ?, birthday = STR_TO_DATE(?, '%d/%m/%Y/') , usertype = ?, status = ? "
                . " WHERE id = ? ";

            if ($stmt = $db->prepare($sql)) {
		
                //binding param
                mysqli_stmt_bind_param($stmt, 'sssssisiii', $user->getUsername(),
                                                            $user->getDisplayname(),
                                                            $user->getEmail(),
                                                            $user->getFirstname(),
                                                            $user->getLastname(),
                                                            $user->getAge(),
                                                            $user->getBirthday(),
                                                            $user->getUsertype(),
                                                            $user->getStatus(),
                                                            $user->getId());
                $flag = $db->execute_statement($stmt);
            }
            $db->close_connection();
        }
        return $flag;
    }

    public function create($user)
    {
        global $db;
        static $flag = false;

         $sql = "INSERT INTO cf_user"
            . "(username, password, displayname, email, "
            . " firstname, lastname, age, birthday, usertype, status) "
            . " VALUES(?, ?, ?, ?, ?, ?, ?, STR_TO_DATE(?, '%d/%m/%Y/') , ?, ?) ";

         if ($stmt = $db->prepare($sql)) {
            //binding param  
            mysqli_stmt_bind_param($stmt, 'ssssssisii', $user->getUsername(),
                                                       crypt($user->getPassword()), // encrypt password
                                                       $user->getDisplayname(),
                                                       $user->getEmail(),
                                                       $user->getFirstname(),
                                                       $user->getLastname(),
                                                       $user->getAge(),
                                                        $user->getBirthday(),
                                                       $user->getUsertype(),
                                                       $user->getStatus());
            $flag = $db->execute_statement($stmt);
        }

        $db->close_connection();
         return $flag;
    }

    public function isDuplicationUser($user)
    {

        global $db;
        static $duplicate = false;

        $sql = "SELECT username, email, status FROM cf_user WHERE username = ? or email = ?";


        if ($stmt = $db->prepare($sql)) {

            //binding param
            mysqli_stmt_bind_param($stmt, 'ss', $user->getUsername(), $user->getEmail());

            $db->execute_statement($stmt);

            $rowMaps = array();
            mysqli_stmt_bind_result($stmt, $rowMaps['username'],
                                           $rowMaps['email'],
                                           $rowMaps['status']);

            $duplicate = $db->fetch_statement($stmt) ? TRUE : false;

            $db->close_connection();
        }

        return $duplicate;
    }

    public function isValidateTokenKey($token)
    {
        global $db;
        $common_service = new common_service();
        return $common_service->validateTokenKey($db, $token, 'token');
    }

}

$user_service = new user_service();
//var_dump($user_service->isValidateTokenKey('abc1234'));

?>